//
//  CommunityCopyViewController.m
//  WeCommunity
//
//  Created by mac on 2017/8/9.
//  Copyright © 2017年 lzy. All rights reserved.
//

#import "CommunityCopyViewController.h"
#import "ComCopyChildViewController.h"
#import "EditTimelineViewController.h"

@interface CommunityCopyViewController ()<SendTimeLineDoneDelegate>

@property (nonatomic,strong) UIBarButtonItem * leftBtnItem;

@end

@implementation CommunityCopyViewController

-(UIBarButtonItem *)leftBtnItem
{
    if (!_leftBtnItem) {
        _leftBtnItem = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:@"addMore"] style:UIBarButtonItemStylePlain target:self action:@selector(processAddMore)];
    }
    return _leftBtnItem;
}

-(void)dealloc
{
    [[NSNotificationCenter defaultCenter] removeObserver:self];
}

- (void)viewDidLoad {
    [super viewDidLoad];

    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(userInfoMangerUpdateUserData:) name:USERLOGIN_CHANGE_NOTIKEY object:nil];
}

-(void)initWithUserInterface
{
    self.channelsArray = @[KNSLocal(@"综合"),KNSLocal(@"入门"),KNSLocal(@"客源"),
                           KNSLocal(@"货源"),KNSLocal(@"销售"),KNSLocal(@"代理"),
                           KNSLocal(@"其他")];
    
    [self.view addSubview:self.chooseTypeView];
    
    [self.view addSubview:self.scrollView];
    
    [self setupChildController];
    
    self.chooseTypeView.channelNameArray = self.channelsArray;
    
    [self setLeftBtnItemStatus];
}

#pragma mark --private Method--初始化子控制器
-(void)setupChildController
{
    self.tableViewArray = [NSMutableArray array];
    for (NSInteger i = 0; i < self.channelsArray.count; i++) {
        ComCopyChildViewController * vc = [[ComCopyChildViewController alloc] init];
        vc.dataType = i;
        vc.view.frame = CGRectMake(KScreen_Width * i, 0,KScreen_Width, self.scrollView.frame.size.height);
        
        [self addChildViewController:vc];
        [self.scrollView addSubview:vc.view];
        
        [self.tableViewArray addObject:vc];
    }
}

#pragma mark -  chooseView delegate
//- (void)chooseChannelWithIndex:(NSInteger)index
//{
//    self.currentIndex = index;
//    
//    [self.scrollView setContentOffset:CGPointMake(self.scrollView.frame.size.width * self.currentIndex, 0) animated:YES];
//    
//    ComCopyChildViewController * vc = self.tableViewArray[self.currentIndex];
//    [vc beginLoadingData];
//    
//    [self canProcessNext];
//}

#pragma mark -  朋友圈发布新数据 刷新界面 delegate
-(void)sendTimeLineContentDoneWithIndex:(NSInteger)index
{
    ComCopyChildViewController * vc = self.tableViewArray[index];
    [vc headerRefreshData];
}

#pragma mark - 点击更多
-(void)processAddMore
{
    if (![self canProcess]) {
        return;
    }
    EditTimelineViewController * editVC = [[EditTimelineViewController alloc] init];
    editVC.delegate = self;
    editVC.timeLineType = TimeLineType_community;
    editVC.dataType = self.currentIndex;
    [self.navigationController pushViewController:editVC animated:YES];
}

#pragma mark - 登陆状态改变
-(void)userInfoMangerUpdateUserData:(NSNotification *)noti
{
    [self setLeftBtnItemStatus];
}

-(void)setLeftBtnItemStatus
{
    if ([UserInfoManger shared].userIsAdmin) {
        self.navigationItem.leftBarButtonItem = self.leftBtnItem;
    }else{
        self.navigationItem.leftBarButtonItem = nil;
    }
}

@end
